/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony.channel;

import java.io.InputStream;
import java.io.OutputStream;

import com.argot.network.TypeEndPoint;
import com.colony.mime.MimePipe;

public class ChannelInternal
{	
	private MimePipe _pipeIn;
	private MimePipe _pipeOut;
	private int _id;
	private Channel _channel;
	private TypeEndPoint _endpoint;
	
	private InputStream _in;
	private OutputStream _out;
	
	protected ChannelInternal(InputStream in, OutputStream out)
	{
		_in = in;
		_out = out;
	}
	
	protected ChannelInternal()
	{
		_pipeIn = new MimePipe();
		_pipeOut = new MimePipe();
	}
	
	protected void setId( int id )
	{
		_id = id;
	}
	
	protected Channel getChannel()
	{
		if ( _in != null || _out != null )
		{
			return null;
		}
		
		if ( _channel == null )
		{
			_channel = new Channel( _pipeIn.inputStream(), _pipeOut.outputStream(), new ChannelIdentifier(_id)  );
		}
		return _channel;
	}
	
	protected TypeEndPoint getEndPoint()
	{
		if ( _in != null || _out != null )
		{
			_endpoint = new Channel( _in, _out, new ChannelIdentifier(_id) );
		}
		if ( _endpoint == null )
		{
			_endpoint = new Channel( _pipeOut.inputStream(), _pipeIn.outputStream(), new ChannelIdentifier(_id) );
		}
		return _endpoint;
	}
}
